<!--
 * @Description:div匀速运动停止
 * @Author: Sun
 * @Date: 2019-07-01 20:05:50
 * @LastEditors: Sun
 * @LastEditTime: 2019-07-06 07:53:18
 -->
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>div匀速运动停止</title>
	<style>
		#div1 {
			width: 100px;
			height: 100px;
			background: green;
			position: absolute;
			left: 600px;
			top: 50px;
		}

		#div2 {
			width: 1px;
			height: 300px;
			position: absolute;
			left: 300px;
			top: 0;
			background: black;
		}

		#div3 {
			width: 1px;
			height: 300px;
			position: absolute;
			left: 150px;
			top: 0;
			background: black;
		}
	</style>
	<script>
		// window.onload = function () {
		function startmove(itarget) {
			var timer = null
			let odiv = document.getElementById("div1")
			clearInterval(timer)
			timer = setInterval(() => {
				var speed=0;
				if(odiv.offsetLeft<itarget){
					speed=7
				}else{
					speed=-7
				}
				if(Math.abs(itarget-odiv.offsetLeft)<=7){
					clearInterval(timer)
					odiv.style.left=itarget+'px'
				}else{
					odiv.style.left=odiv.offsetLeft+speed+'px'
				}
				// var speed = (300 - odiv.offsetLeft) / 10
				// speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed)
				// odiv.style.left = odiv.offsetLeft + speed + 'px'
				// document.title = odiv.offsetLeft + '.' + speed
			}, 40)
		}
		// }
	</script>
</head>

<body>
	<div>
		Math.ceil(-9.8) 向上 -9 <br>
		Math.floor(4.8) 向下 4 <br>
		Math.abs(-4.8) 绝对值 4.8 <br>
		速度=（目标值-当前值）/缩放比例 <br>
		缓冲运动不忘了取整
	</div>
	<input type="button" value="到100" onclick="startmove(600)">
	<input type="button" value="到100" onclick="startmove(300)">
	<input type="button" value="到150" onclick="startmove(150)">
	<div id="div1">
		<span>分享到</span>
	</div>
	<div id="div2"></div>
	<div id="div3"></div>
</body>

</html>